require 'rubygems'
require 'activerecord'
require 'digest/sha1'
require 'db_connect_info'

DBConnectInfo::connect()

# アカウント情報
#
# Author:: krdlab
#
class Account < ActiveRecord::Base

  # TODO 制限

  def self.new_account(name, password)
    return create(:name => name,
                  :password => to_digest(password),
                  :ticket => create_ticket)
  end

  def self.login(name, password)
    dg = to_digest(password)
    ac = find(:first,
              :conditions => ["name = ? and password = ?", name, dg])
    if not ac.nil? then
      ac.update_attribute(:ticket, create_ticket)
    end
    return ac
  end

  # get_by_ticket -> try_logout
  def logout
    update_attribute(:ticket, nil)
  end

  def self.create_ticket
    to_digest(Time.now.to_s)
  end

  def self.to_digest(raw)
    Digest::SHA1.hexdigest(raw)
  end
end

if __FILE__ == $0
end

